<?php
class Sms {
	private $registry;
	private $regist = 'http://sdkhttp.eucp.b2m.cn/sdkproxy/regist.action'; 
	private $logout = 'http://sdkhttp.eucp.b2m.cn/sdkproxy/logout.action'; 
	private $send_sms = 'http://sdkhttp.eucp.b2m.cn/sdkproxy/sendsms.action'; 
	private $send_time_sms = 'http://sdkhttp.eucp.b2m.cn/sdkproxy/sendtimesms.action';
	private $query_balance = 'http://sdkhttp.eucp.b2m.cn/sdkproxy/querybalance.action';
	private $mask_phone = "/^13[0-9]{1}[0-9]{8}$|15[0189]{1}[0-9]{8}$|189[0-9]{8}$/";
	private $sign_data = array('cdkey' => CONF_SMS_CDKEY, 'password' => CONF_SMS_PASSWORD);
	private $sign_name = " 【旅人蕉生活网】";
	private $active = true;

  	public function __construct($registry) {
  		$this->registry = $registry;
		if ( !CONF_SMS_ACTIVE ) {
			$this->active = false;
		}
		else {
			
		}
  	}
  	
  	public function __get($key) {
  		return $this->registry->get($key);
  	}
  	
  	public function __set($key, $value) {
  		$this->registry->set($key, $value);
  	}
  	
  	public function getBalance() {
  		return $this->curlPost($this->query_balance, $this->sign_data);
  	}
  	/**
  	 * 发送短信
  	 *
  	 * @param array/string $phone: 电话组或电话,
  	 * @param string $message: 信息 ,
  	 * @return string
  	 *
  	 * @author zhuzhenguo
  	 * Modified: 2012/10/31 14:42
  	 */
  	public function sendSms($phone, $messageId, $data = array()) {
  		$message = '';
  		
  		$this->config->load('sms');
  		
  		$_confSmsText = $this->config->get('smsText');
  		
  		if ( !isset($_confSmsText[$messageId]) ) {
  			return -6;
  		}
  		
  		if ( !$this->active ) {
  			return -5;
  		}
  		if ( !isset($phone) ) {
  			return -1;
  		}
  		if ( !isset($data) ) {
  			return -2;
  		}
  		if ( is_array($phone) ) {
  			foreach ($phone as $item) {
  				if ( !preg_match($this->mask_phone, $item) ) {
  					return -3;
  				}
  			}
  			$phone = implode(',', $phone);
  		}
  		else {
  			if ( !preg_match($this->mask_phone, $phone) ) {
  				return -3;
  			}
  		}
  		
  		$message = strtr($_confSmsText[$messageId], $data);
  		if (utf8_strlen($message) < 3) {
  			return -4;
  		}
  		
  		$_data = array('phone' => $phone, 'message' => $message);
  		$_data = array_merge($this->sign_data, $_data);
  		
  		return $this->curlPost($this->send_sms, $_data);
  	}
  	/**
  	 * curl Post方式提交数据
  	 *
  	 * @param string $url: 请求地址,
  	 * @param array $post[string 参数名] (
  	 * string value: 参数值
  	 * ),
  	 * @param array $options: cURL参数,
  	 * @return string 
  	 *
  	 * @author zhuzhenguo
  	 * Modified: 2012/10/31 11:42
  	 */
  	private function curlPost($url, array $post = NULL, array $options = array()) {
  		$post['message'] = isset($post['message']) ? $post['message'] . $this->sign_name : '';
  		
  		$defaults = array(
  				CURLOPT_POST => 1,
  				CURLOPT_HEADER => 0,
  				CURLOPT_URL => $url,
  				CURLOPT_FRESH_CONNECT => 1,
  				CURLOPT_RETURNTRANSFER => 1,
  				CURLOPT_FORBID_REUSE => 1,
  				CURLOPT_TIMEOUT => 4,
  				CURLOPT_POSTFIELDS => http_build_query($post)
  		);
  		
  		$ch = curl_init();
  		curl_setopt_array($ch, ($options + $defaults));
  		$result = curl_exec($ch);
  		curl_close($ch);
  		
  		return simplexml_load_string(trim($result));
  	}
}
?>